/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ibmmq_detect.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/30 by jikw                                 */
/*******************************************************************/
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frct_comm.h"
#include "../../../src/module/ibmmq/config.h"
#include "../test_ibmmq_comm.h"
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <ibmmq/cnaps2.h>
//#include "../../../src/exec/framework/frreport_session_mng.c"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

extern int ibmmq_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static void case1(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_packet);

    return;
}

static void case2(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_conn);

    return;
}

static void case3(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));

    p_st_packet->ui_payload_len = 1;

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case4(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(p_st_packet));
    p_st_packet->p_payload = (char *)frmalloc(5);
    memset(p_st_packet->p_payload, 0x00, 5);

    p_st_packet->ui_payload_len = 100*1048576 + 1;

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case5(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)frmalloc(5);
    memset(p_st_packet->p_payload, 0x00, 5);

    p_st_packet->ui_payload_len = 100;
    memcpy(p_st_packet->p_payload, "TSC ", 4);

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case6(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)frmalloc(5);
    memset(p_st_packet->p_payload, 0x00, 5);

    p_st_packet->ui_payload_len = 8;
    memcpy(p_st_packet->p_payload, "TSH ", 4);

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case7(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_buf = NULL;
    int i_len = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));

    p_file = fopen((char *)"ibmmq_detect7.pcap", "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_buf = (char *)frmalloc(i_len + 1);
    memset(p_buf, 0x00, i_len + 1);
    fread(p_buf, 1, i_len, p_file);
    fclose(p_file);

    p_st_packet->ui_payload_len = 100;
    p_st_packet->p_payload = p_buf;

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_buf);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case8(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_buf = NULL;
    int i_len = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));

    p_file = fopen((char *)"ibmmq_detect8.pcap", "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_buf = (char *)frmalloc(i_len + 1);
    memset(p_buf, 0x00, i_len + 1);
    fread(p_buf, 1, i_len, p_file);
    fclose(p_file);

    p_st_packet->ui_payload_len = 9;
    p_st_packet->p_payload = p_buf;

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_buf);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case9(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_buf = NULL;
    int i_len = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));

    p_file = fopen((char *)"ibmmq_detect7.pcap", "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_buf = (char *)frmalloc(i_len + 1);
    memset(p_buf, 0x00, i_len + 1);
    fread(p_buf, 1, i_len, p_file);
    fclose(p_file);

    p_st_packet->ui_payload_len = i_len;
    p_st_packet->p_payload = p_buf;

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    frfree(p_buf);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case10(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_buf = NULL;
    int i_len = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));

    p_file = fopen((char *)"ibmmq_detect10.pcap", "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_buf = (char *)frmalloc(i_len + 1);
    memset(p_buf, 0x00, i_len + 1);
    fread(p_buf, 1, i_len, p_file);
    fclose(p_file);

    p_st_packet->ui_payload_len = i_len;
    p_st_packet->p_payload = p_buf;

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_buf);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case11(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_buf = NULL;
    int i_len = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));

    p_file = fopen((char *)"ibmmq_detect11.pcap", "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_buf = (char *)frmalloc(i_len + 1);
    memset(p_buf, 0x00, i_len + 1);
    fread(p_buf, 1, i_len, p_file);
    fclose(p_file);

    p_st_packet->ui_payload_len = i_len;
    p_st_packet->p_payload = p_buf;

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_buf);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

static void case12(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_buf = NULL;
    int i_len = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = 1;
    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    memset(p_st_packet, 0x00, sizeof(Apm_packet));

    p_file = fopen((char *)"ibmmq_detect11.pcap", "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_buf = (char *)frmalloc(i_len + 1);
    memset(p_buf, 0x00, i_len + 1);
    fread(p_buf, 1, i_len, p_file);
    fclose(p_file);

    p_st_packet->ui_payload_len = i_len;
    p_st_packet->p_payload = p_buf;

    i_ret = ibmmq_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_buf);
    frfree(p_st_packet);
    frfree(p_st_conn);

    return;
}

int add_suite_ibmmq_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ibmmq_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL ||
        CU_add_test(pSuite, "case7", case7) == NULL ||
        CU_add_test(pSuite, "case8", case8) == NULL ||
        CU_add_test(pSuite, "case9", case9) == NULL ||
        CU_add_test(pSuite, "case10", case10) == NULL ||
        CU_add_test(pSuite, "case11", case11) == NULL ||
        CU_add_test(pSuite, "case12", case12) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
